package com.mystudy.leetcode.problem.tree.p_257;

import com.mystudy.leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: infoalgorithm
 * @description: 二叉树的所有路径
 * @author: zhouzhilong
 * @create: 2019-07-24 16:21
 **/
public class Solution {
    List<String> result = new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {
        if(root == null){
            return result;
        }
        //当节点的左孩子与右孩子皆为空的情况下此条路径成立，可以加入List
        binaryTreePath(root,"");
        return result;
    }


    private void binaryTreePath(TreeNode node,String parentPath){
        if(node == null){
            return;
        }
        if(node.left == null && node.right == null){
            result.add(parentPath+node.val);
        }else{
            binaryTreePath(node.left,parentPath+node.val+"->");
            binaryTreePath(node.right,parentPath+node.val+"->");
        }

    }


}
